package OSMS.action.indexfunc;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.CustomerDao;
import OSMS.dao.GoodsDao;
import OSMS.entity.Customers;
import OSMS.entity.Goods;
import OSMS.entity.Types;

public class loadIndexPartsServlet extends HttpServlet{
	GoodsDao gdao = new GoodsDao();
	CustomerDao cdao = new CustomerDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
	
//----------中间栏随机推荐商品----------//
		ArrayList<Goods> goodsList_random = new ArrayList<Goods>();
		try {
			goodsList_random = (ArrayList<Goods>)gdao.orderGoodsByrandom(8);		//此处修改随机出现的商品数量
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("goodsList_random", goodsList_random);

//----------底部滚动随机推荐商品----------//
		ArrayList<Goods> goodsList_random_scroll = new ArrayList<Goods>();
		try {
			goodsList_random_scroll = (ArrayList<Goods>)gdao.orderGoodsByrandom(6);		//此处修改随机出现的商品数量
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("goodsList_random_scroll", goodsList_random_scroll);

//--------------边栏热销榜-------------//
		ArrayList<Goods> goodsList_sales = new ArrayList<Goods>();
		try {
			goodsList_sales = (ArrayList<Goods>)gdao.orderGoodsBySales();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(goodsList_sales.size());
		session.setAttribute("goodsList_sales", goodsList_sales);
		
		
//----------------边栏热搜榜----------//
		ArrayList<Goods> goodsList_search = new ArrayList<Goods>();
		try {
			goodsList_search = (ArrayList<Goods>)gdao.orderGoodsBySearch();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("goodsList_search", goodsList_search);
	
//------------待补充：边栏“猜你喜欢”榜单-----//
		Customers cus = new Customers();
		cus = (Customers)session.getAttribute("cus");
		Types type = new Types();
		try {
			type = cdao.mostLiketype(cus.getCid());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayList<Goods> goodsList_clike = new ArrayList<Goods>();
		try {
			goodsList_clike = (ArrayList<Goods>)gdao.searchGoodsByGtype(type.getTid());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.setAttribute("goodsList_clike", goodsList_clike);

		response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/index/index.jsp");	
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
